package com.ihisun.tmhcms.core.service.doctorservice.impl;

import com.ihisun.tmhcms.core.dao.housekeeperdao.HousekeeperDao;
import com.ihisun.tmhcms.core.dao.mechanismdao.MechanismDao;
import com.ihisun.tmhcms.core.dao.doctordao.DoctorDao;
import com.ihisun.tmhcms.core.dao.doctordao.DoctorMechanismDao;
import com.ihisun.tmhcms.core.dao.doctordao.PracticeDao;
import com.ihisun.tmhcms.core.dao.projectdao.ProjectClassDao;
import com.ihisun.tmhcms.core.dao.projectdao.ProjectDao;
import com.ihisun.tmhcms.core.dto.doctordto.SearchDoctorDto;
import com.ihisun.tmhcms.core.service.doctorservice.DoctorService;
import com.ihisun.tmhcms.core.vo.*;
import com.ihisun.tmhcms.utils.PageBean;
import com.ihisun.tmhcms.utils.ResponseVo;
import com.ihisun.tmhcms.utils.Testtrans;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.sql.SQLData;
import java.util.*;
@Slf4j
@Service
public class DoctorServiceImpl implements DoctorService {
    @Resource
    DoctorDao doctorDao;
    @Resource
    DoctorMechanismDao doctorMechanismDao;
    @Resource
    PracticeDao practiceDao;
    @Resource
    ProjectDao projectDao;
    @Resource
    MechanismDao mechanismDao;
    @PersistenceContext
    EntityManager entityManager;
    @Resource
    ProjectClassDao projectClassDao;

    @Override
    public ResponseVo findByDoctorInfo(String doctorId) {
        List<MechanismVo> byMechanismId = null;
        DoctorVo byDoctorId1 = doctorDao.findByDoctorId(doctorId);
        List<DoctorMechanismVo> byDoctorId = doctorMechanismDao.findByDoctorId(doctorId);
        if (byDoctorId != null && !byDoctorId.isEmpty()) {
            for (int i = 0; i < byDoctorId.size(); i++) {
                byMechanismId = mechanismDao.findByMechanismId(byDoctorId.get(i).getMechanismId());
            }
        }
        PracticeVo byDoctorId2 = practiceDao.findByDoctorId(doctorId);
        List<ProjectVo> byDoctorId3 = projectDao.findByDoctorId(doctorId);
        if (byDoctorId1 != null) {
            if (byMechanismId != null) {
                byDoctorId1.setMechanismVos(byMechanismId);
            }
            if (byDoctorId2 != null) {
                byDoctorId1.setPracticeVo(byDoctorId2);
            }
            if (byDoctorId3 != null && !byDoctorId3.isEmpty()) {
                byDoctorId1.setProjectVo(byDoctorId3);
            }
            return ResponseVo.ok(byDoctorId1);
        }
        return ResponseVo.isNull(byDoctorId1);


    }

    @Override
    public ResponseVo findByDoctorMechanism(String doctorId) {
        List<MechanismVo> byDoctorMechanism = mechanismDao.findByDoctorMechanism(doctorId);
        if (byDoctorMechanism != null && !byDoctorMechanism.isEmpty()) {
            return ResponseVo.ok(byDoctorMechanism);
        }
        return ResponseVo.isNull(byDoctorMechanism);
    }

    @Override
    public ResponseVo findByState(String doctorId, Integer state) {
        //全部机构
        List<MechanismVo> byState = mechanismDao.findByState(state);
        //已经绑定的机构
        List<MechanismVo> byDoctorMechanism = mechanismDao.findByDoctorMechanism(doctorId);
        if (byDoctorMechanism != null) {
            for (int i = 0; i < byState.size(); i++) {
                for (int j = 0; j < byDoctorMechanism.size(); j++) {
                    if (byState.get(i).getMechanismId().equals(byDoctorMechanism.get(j).getMechanismId())) {
                        byState.remove(i);
                    }
                }
            }
            return ResponseVo.ok(byState);
        }
        return ResponseVo.ok(byState);
    }

    @Override
    public DoctorVo updateDoctorState(String doctorId) {
        DoctorVo byDoctorId = doctorDao.findByDoctorId(doctorId);
        return byDoctorId;
    }

    @Override
    public ResponseVo searchDoctor(SearchDoctorDto searchDoctorDto) {
        StringBuilder sql = new StringBuilder("select * from d_doctor where state !=3");
        if (searchDoctorDto.getName() != null &&!"".equals(searchDoctorDto.getName())) {
            sql.append(" or  doctor_name like concat('%','" + searchDoctorDto.getName() + "','%')");
        }
        if (searchDoctorDto.getPhone() != null && !"".equals(searchDoctorDto.getPhone())) {
            sql.append(" or phone like concat('%'," + searchDoctorDto.getPhone() + ",'%')");
        }
        if (searchDoctorDto.getCity() != null && !"".equals(searchDoctorDto.getCity())) {
            sql.append(" or address=" + searchDoctorDto.getCity() +"");
        }
        if (searchDoctorDto.getBegoodat() != null && !"".equals(searchDoctorDto.getBegoodat())) {
            sql.append(" or doctor_id in (select doctor_id from p_project where project_type_id in (select project_type_id from p_project_class where project_class_name like concat('%','" + searchDoctorDto.getBegoodat() + "','%')))");
        }
        if (searchDoctorDto.getRecommend() != null && !"".equals(searchDoctorDto.getRecommend())) {
            sql.append(" or recommend=" + searchDoctorDto.getRecommend() + "");
        }
        if (searchDoctorDto.getSex() != null && !"".equals(searchDoctorDto.getSex())) {
            sql.append("  or sex=" + searchDoctorDto.getSex() + "");
        }
        if (searchDoctorDto.getState() != null && !"".equals(searchDoctorDto.getState())) {
            sql.append(" or state=" + searchDoctorDto.getState()+"");
        }
        if (searchDoctorDto.getTitle() != null && !"".equals(searchDoctorDto.getTitle())) {
            sql.append(" or title like concat('%'," + searchDoctorDto.getTitle() + ",'%')");
        }
        sql.append(" order by recommend desc limit " + (searchDoctorDto.getPageNum() - 1) * 10 + "," + searchDoctorDto.getPageSize() + "");

        List<DoctorVo> resultList = entityManager.createNativeQuery(sql.toString(), DoctorVo.class).getResultList();
        StringBuilder countSql = new StringBuilder("select count(*) from d_doctor where state !=3");


            if (searchDoctorDto.getName() != null && !"".equals(searchDoctorDto.getName())) {
                countSql.append(" or  doctor_name like concat('%','" + searchDoctorDto.getName() + "','%')");
            }
            if (searchDoctorDto.getPhone() != null && !"".equals(searchDoctorDto.getPhone())) {
                countSql.append(" or phone like concat('%'," + searchDoctorDto.getPhone() + ",'%')");
            }
            if (searchDoctorDto.getCity() != null && !"".equals(searchDoctorDto.getCity())) {
                countSql.append(" or address =" + searchDoctorDto.getCity() + "");
            }
            if (searchDoctorDto.getBegoodat() != null && !"".equals(searchDoctorDto.getBegoodat())) {
                countSql.append(" or doctor_id in (select doctor_id from p_project where project_type_id in (select project_type_id from p_project_class where project_class_name like concat('%','" + searchDoctorDto.getBegoodat() + "','%')))");
            }
            if (searchDoctorDto.getRecommend() != null && !"".equals(searchDoctorDto.getRecommend())) {
                countSql.append(" or recommend= "+ searchDoctorDto.getRecommend() + "");
            }
            if (searchDoctorDto.getSex() != null && !"".equals(searchDoctorDto.getSex())) {
                countSql.append("  or sex=" + searchDoctorDto.getSex() + "");
            }
            if (searchDoctorDto.getState() != null && !"".equals(searchDoctorDto.getState())) {
                countSql.append(" or state= " + searchDoctorDto.getState() + "");
            }
            if (searchDoctorDto.getTitle() != null && !"".equals(searchDoctorDto.getTitle())) {
                countSql.append(" or title like concat('%'," + searchDoctorDto.getTitle() + ",'%')");
            }
            countSql.append(" order by recommend desc");

        log.info("sql:"+sql.toString());
        log.info("countSql:"+countSql.toString());

        BigInteger singleResult =(BigInteger) entityManager.createNativeQuery(countSql.toString()).getSingleResult();
        PageBean pageBean=new PageBean(searchDoctorDto.getPageNum(),singleResult.intValue(),searchDoctorDto.getPageSize(),resultList);
        return ResponseVo.ok(pageBean);
    }

    @Override
    public ResponseVo findeByProjectClassName() {
        List<ProjectClassVo> byStateNotGroupByProjectClassName = projectClassDao.findByStateNot();
        return ResponseVo.ok(byStateNotGroupByProjectClassName);
    }


}
